/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ibrae.excelparser1.mainlogics.fileworkers;

import ru.ibrae.excelparser1.excelstorageentities.FileNameInfo;
import ru.ibrae.excelparser1.excelstorageentities.constructionresults.VerificationResult;
import ru.ibrae.excelparser1.excelstorageentities.entities.MessageLevel;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author murr
 */
public class FileNameParser {
    //~ Static fields/initializers ---------------------------------------------

    private static final SimpleDateFormat NAME_DATE_FORMAT = new SimpleDateFormat(
            "yy-MM-dd");

    //~ Methods ----------------------------------------------------------------
    public static FileNameVerificaionResult checkSelectedFileName(String name) {
        VerificationResult result = new VerificationResult(
                "",
                MessageLevel.FILE_FATAL);
        FileNameVerificaionResult fileNameVerificaionResult = new FileNameVerificaionResult(
                null,
                null);
        name = name.replaceAll(" ", "");
        name = name.replaceAll(".xls", "");
        name = name.replaceAll("\n", "");

        String firstSplit[] = name.split("_");
        String ogrn = null;
        Date beginning = null;
        Date end = null;
        Integer iteration = null;

        if (( firstSplit.length == 3 ) || ( firstSplit.length == 4 )) {
            if (firstSplit[0].length() == 13) {
                try {
                    for (int i = 0; i < firstSplit[0].length(); i++) {
                        char c = firstSplit[0].charAt(i);
                        Integer.parseInt(String.valueOf(c));
                    }

                    ogrn = firstSplit[0];

                    try {
                        beginning = NAME_DATE_FORMAT.parse(firstSplit[1]);
                        end = NAME_DATE_FORMAT.parse(firstSplit[2]);

                        if (end.before(beginning)) {
                            result.setMessage(
                                    "Дата начала периода раньше даты конца периода");
                        }
                        try {
                            iteration = Integer.valueOf(firstSplit[3]);
                        }
                        catch (Exception e) {
                            iteration = null;
                        }
                    }
                    catch (Exception ex) {
                        result.setMessage(
                                "Некорректно оФормлена дата начала или конца периода (должна быть в формате гггг-мм-дд)");
                    }
                }
                catch (Exception e) {
                    result.setMessage(
                            "Некорректные знаки в ОГРН (должен состоять из 13 цифр)");
                }
            } else {
                result.setMessage("Неправильная длина ОГРН");
            }
        } else {
            result.setMessage(
                    "Не обнаружен разделитель между ОГРН и периодом либо неправильный формат имени файла");
        }

        if (!result.getMessage().equals("")) {
            fileNameVerificaionResult.setResult(result);

            return fileNameVerificaionResult;
        }

        result.setMessageLevel(MessageLevel.NO_ERRORS);
        fileNameVerificaionResult = new FileNameVerificaionResult(new FileNameInfo(iteration, ogrn, beginning, end), result);

        return fileNameVerificaionResult;
    }

    //~ Inner Classes ----------------------------------------------------------
    public static class FileNameVerificaionResult {

        FileNameInfo fileNameInfo;
        VerificationResult result;

        public FileNameVerificaionResult(FileNameInfo fileNameInfo, VerificationResult result) {
            this.fileNameInfo = fileNameInfo;
            this.result = result;
        }

        public FileNameInfo getFileNameInfo() {
            return fileNameInfo;
        }

        public VerificationResult getResult() {
            return result;
        }

        public void setFileNameInfo(FileNameInfo fileNameInfo) {
            this.fileNameInfo = fileNameInfo;
        }

        public void setResult(VerificationResult result) {
            this.result = result;
        }

        public void setBeginning(Date start) {
            fileNameInfo.setStart(start);
        }

        public void setOgrn(String ogrn) {
            fileNameInfo.setOgrn(ogrn);
        }

        public void setIteration(Integer iteration) {
            fileNameInfo.setIteration(iteration);
        }

        public void setEnd(Date end) {
            fileNameInfo.setEnd(end);
        }

        public Date getStart() {
            return fileNameInfo.getStart();
        }

        public String getOgrn() {
            return fileNameInfo.getOgrn();
        }

        public Integer getIteration() {
            return fileNameInfo.getIteration();
        }

        public Date getEnd() {
            return fileNameInfo.getEnd();
        }
    }
}
